﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using QuanCafe.DataLayer;

namespace QuanCafe.BusinessLayer
{
    public class Item
    {
        #region Local Variable
        public string activity;
        public int ItemID;
        public string Email;
        public string Password;
        public int ItemTypeID;
        public int LevelID;
        public string ItemTitle;
        public string ItemDesc;
        public string ItemContent;
        public string Address;
        public int DistrictID;
        public int ProvinceID;
        public string LinkMapImage;
        public string WebLink;
        public string Tag;
        public string TagWithoutSign;
        public int Permission;
        public int Status;
        public DateTime PostDate;
        public string Creator;

        #endregion Local Variable

        SQLHelper db = new SQLHelper();

        #region Method

        public DataTable GetAll()
        {
            try
            {
                db.AddParameter("@Activity", "GetAll", SqlDbType.NVarChar);
                return db.ExecuteReader("SP_Item", CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public DataRow GetByID()
        {
            try
            {
                db.AddParameter("@Activity", "GetByID", SqlDbType.NVarChar);
                db.AddParameter("@ItemID", ItemID, SqlDbType.Int);
                DataTable dtb = db.ExecuteReader("SP_Item", CommandType.StoredProcedure);

                if (dtb.Rows.Count > 0)
                    return dtb.Rows[0];
                else
                    return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string InsertUpdate()
        {
            try
            {
                db.AddParameter("@Activity", activity, SqlDbType.NVarChar);
                db.AddParameter("@ItemID", ItemID, SqlDbType.Int);
                db.AddParameter("@Email", Email, SqlDbType.NVarChar);
                db.AddParameter("@Password", Password, SqlDbType.NVarChar);
                db.AddParameter("@ItemTypeID", ItemTypeID, SqlDbType.Int);
                db.AddParameter("@LevelID", LevelID, SqlDbType.Int);
                db.AddParameter("@ItemTitle", ItemTitle, SqlDbType.NVarChar);
                db.AddParameter("@ItemDesc", ItemDesc, SqlDbType.NVarChar);
                db.AddParameter("@ItemContent", ItemContent, SqlDbType.NVarChar);
                db.AddParameter("@Address", Address, SqlDbType.NVarChar);
                db.AddParameter("@DistrictID", DistrictID, SqlDbType.Int);
                db.AddParameter("@ProvinceID", ProvinceID, SqlDbType.Int);
                db.AddParameter("@LinkMapImage", LinkMapImage, SqlDbType.NVarChar);
                db.AddParameter("@WebLink", WebLink, SqlDbType.NVarChar);
                db.AddParameter("@Tag", Tag, SqlDbType.NVarChar);
                db.AddParameter("@TagWithoutSign", TagWithoutSign, SqlDbType.NVarChar);
                db.AddParameter("@Permission", Permission, SqlDbType.Int);
                db.AddParameter("@Status", Status, SqlDbType.Int);
                //db.AddParameter("@PostDate", PostDate, SqlDbType.DateTime);
                db.AddParameter("@Creator", Creator, SqlDbType.NVarChar);
                return db.ExecuteScalar("SP_Item", CommandType.StoredProcedure).ToString();
            }
            catch
            {
                return null;
            }
        }

        #endregion Method

        internal void ChangeActive()
        {
            try
            {
                db.AddParameter("@Activity", "ChangeActive", SqlDbType.NVarChar);
                db.AddParameter("@ItemID", ItemID, SqlDbType.Int);
                db.ExecuteScalar("SP_Item", CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
